#!/usr/bin/env sh

# export VERSION=`curl -s https://api.github.com/repos/labring/sealos/releases/latest | grep -oE '"tag_name": "[^"]+"' | head -n1 | cut -d'"' -f4`
# wget https://ghproxy.net/https://github.com/labring/sealos/releases/download/${VERSION}/sealos_${VERSION#v}_linux_amd64.tar.gz \
#    && tar zxvf sealos_${VERSION#v}_linux_amd64.tar.gz sealos && chmod +x sealos && mv sealos /usr/bin

# Install Sealos

# if apt installed, use `apt` to install
if [ -x "$(command -v apt)" ]; then
  echo "deb [trusted=yes] https://apt.fury.io/labring/ /" | tee /etc/apt/sources.list.d/labring.list
  apt update
  apt install iptables host -y
  apt install sealos -y

  # fix /etc/hosts overwrite bug in ubuntu while restarting
  sed -i "/update_etc_hosts/c \\ - ['update_etc_hosts', 'once-per-instance']" /etc/cloud/cloud.cfg && touch /var/lib/cloud/instance/sem/config_update_etc_hosts
fi

# if yum installed, use `yum` to install
if [ -x "$(command -v yum)" ]; then
  cat >/etc/yum.repos.d/labring.repo <<EOF
[fury]
name=labring Yum Repo
baseurl=https://yum.fury.io/labring/
enabled=1
gpgcheck=0
EOF
  # yum update
  yum clean all
  yum install -y bind-utils iptables
  yum install sealos -y
fi

ARCH=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/)
echo "ARCH: $ARCH"

# if sealos not installed
if [ ! -x "$(command -v sealos)" ]; then
  echo "sealos not installed"
  exit 1
fi
